<?php

namespace app\modules\weixin\controllers;

use app\common\services\StaticServices;
use app\models\IntegralRecord;
use app\models\Member;
use app\models\MemberIntegral;
use app\models\MemberNotice;
use app\models\Recommend;
use app\modules\weixin\controllers\common\BaseController;
use Yii;
use yii\base\Exception;
use yii\web\Response;

class MemberController extends BaseController {
  /**
   * 个人中心
   * @return array
   */
  public function actionCenter()
  {
    Yii::$app->response->format = Response::FORMAT_JSON;
    try {
      $member_id = intval(Yii::$app->request->get('member_id'));
      if (!$member_id) {
        throw new Exception('参数有误');
      }
      $member_info = Member::find()->where(['id' => $member_id])->one();
      $member_user_integral = StaticServices::getMemberUseIntegral($member_id);
      $member_total_integral = intval($member_user_integral) + intval($member_info['member_integral']);
      $record_count = IntegralRecord::getNoExchange($member_id);
      $data = [
          'member_user_integral'  => $member_user_integral,
          'member_total_integral' => $member_total_integral,
          'record_count'          => $record_count,
          'member_integral'       => $member_info['member_integral'],
          'member_name'           => $member_info['member_name'],
          'member_avatar'         => isset($member_info['header_url']) ? $member_info['header_url'] : '',
          'member_address'        => $member_info['member_address'],
      ];
      return ['code' => 200, 'data' => $data];
    } catch (Exception $e) {
      return ['code' => -200, 'msg' => $e->getMessage()];
    }
  }

  /**
   *
   * 公告
   * @return array
   */
  public function actionNotice()
  {
    Yii::$app->response->format = Response::FORMAT_JSON;
    try {
      $p = intval(Yii::$app->request->get('p', 1));
      $notice_info = MemberNotice::find()->where(['notice_status' => 1]);

      $page_size = 5;
      $total_count = $notice_info->count();
      $total_page = ceil($total_count / $page_size);

      $list = $notice_info->offset(($p - 1) * $page_size)->orderBy(['update_time' => SORT_DESC])->limit($page_size)->all();
      return ['code' => 200, 'data' => $list,'total_page'=>$total_page];
    } catch (Exception $e) {
      return ['code' => -200, 'msg' => $e->getMessage()];
    }
  }

  /**
   *
   * 积分流水
   * @return array
   */
  public function actionIntegralDetails()
  {
    Yii::$app->response->format = Response::FORMAT_JSON;
    try {
      $member_id = intval(Yii::$app->request->post('member_id'));
      $integral_type = Yii::$app->request->post('integral_type');
      $p = intval(Yii::$app->request->post('p', 1));

      if (!$member_id) {
        throw new Exception('参数有误');
      }

      $page_size = 5;

      if ($integral_type == 'use') {
        $use_integral = IntegralRecord::find()->where(['member_id' => $member_id]);

        $total_count = $use_integral->count();
        $total_page = ceil($total_count / $page_size);

        $list = $use_integral->offset(($p - 1) * $page_size)->orderBy(['create_time' => SORT_DESC])->limit($page_size)->all();
        return ['code' => 200, 'data' => $list, 'total_page' => $total_page];
      }

      if ($integral_type == 'get') {
        $get_integral = MemberIntegral::find()->where(['member_id' => $member_id]);

        $total_count = $get_integral->count();
        $total_page = ceil($total_count / $page_size);

        $list = $get_integral->offset(($p - 1) * $page_size)->orderBy(['create_time' => SORT_DESC])->limit($page_size)->all();
        return ['code' => 200, 'data' => $list, 'total_page' => $total_page];
      }
    } catch (Exception $e) {
      return ['code' => -200, 'msg' => $e->getMessage()];
    }
  }

  public function actionMemberInfo()
  {
    if (Yii::$app->request->isPost) {
      Yii::$app->response->format = Response::FORMAT_JSON;
      try {
        $member_id = intval(Yii::$app->request->post('member_id', 0));
        if ($member_id) {
          $member_info = Member::find()->select(['id', 'header_url', 'member_address', 'member_name'])->where(['id' => $member_id])->one();
          return ['code' => 200, 'data' => $member_info];
        } else {
          throw new Exception('数据有误');
        }
      } catch (Exception $e) {
        return ['code' => -200, 'msg' => $e->getMessage()];
      }
    }
  }

}
